package ru.ipccenter.resource;

import org.apache.commons.collections.bidimap.TreeBidiMap;
/**
 *
 * @author Mari
 * this class is intended for converting
 * from russian into english end vice versa
 */
public class LayOut {
    private TreeBidiMap keyboard = new TreeBidiMap();
    public LayOut(){
        keyboard.put('q','й');
        keyboard.put('w','ц');
        keyboard.put('e','у');
        keyboard.put('r','к');
        keyboard.put('t','е');
        keyboard.put('y','н');
        keyboard.put('u','г');
        keyboard.put('i','ш');
        keyboard.put('o','щ');
        keyboard.put('p','з');
        keyboard.put('a','ф');
        keyboard.put('s','ы');
        keyboard.put('d','в');
        keyboard.put('f','а');
        keyboard.put('g','п');
        keyboard.put('h','р');
        keyboard.put('j','о');
        keyboard.put('k','л');
        keyboard.put('l','д');
        keyboard.put('z','я');
        keyboard.put('x','ч');
        keyboard.put('c','с');
        keyboard.put('v','м');
        keyboard.put('b','и');
        keyboard.put('n','т');
        keyboard.put('m','ь');
        keyboard.put('[','х');
        keyboard.put(']','ъ');
        
        keyboard.put(',','б');
        keyboard.put('.','ю');
        keyboard.put('/','.');
        keyboard.put(';','ж');
        keyboard.put('\'','э');
    }
    
    public String convertRuEng(String query) {
        String oldQuery = query.toLowerCase();
        StringBuilder newQuery = new StringBuilder(100);
        
        for (int i = 0; i < query.length();++i) {            
            try {
                newQuery.append(keyboard.getKey(oldQuery.charAt(i)));
            } catch (NullPointerException e) {
                newQuery.append(oldQuery.charAt(i));
            }            
        }
        return newQuery.toString();
    }

    public String convertEngRu(String query){
        
        String oldQuery = query.toLowerCase();
        StringBuilder newQuery = new StringBuilder(100);
        
        for (int i = 0; i < query.length();++i) {
            try {
                newQuery.append(keyboard.get(oldQuery.charAt(i)));
            } catch (NullPointerException e) {
                newQuery.append(oldQuery.charAt(i));                
            }            
        }
        return newQuery.toString();
    }
    
}
